package com.dm.contriller;


import com.alibaba.fastjson.JSONObject;
import com.dm.client.dm_item.RestDmItemClient;
import com.dm.client.dm_scheduler.RestDmSchedulerSeatPriceClient;
import com.dm.client.dm_user.RestDmLinkUserClient;
import com.dm.client.dm_user.RestDmUserClient;
import com.dm.common.Constants;
import com.dm.common.EmptyUtils;
import com.dm.common.KafKaUtil;
import com.dm.common.RedisUtils;
import com.dm.dto.Dto;
import com.dm.dto.DtoUtil;
import com.dm.exception.ErrorCode;
import com.dm.pojo.dm_item.DmItem;
import com.dm.pojo.dm_scheduler.DmSchedulerSeatPrice;
import com.dm.pojo.dm_user.DmLinkUser;
import com.dm.service.OrderService;
import com.dm.vo.CreateOrderVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user/api")
public class UserOrderController {

    @Resource
   private OrderService orderService;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RestDmLinkUserClient restDmLinkUserClient;

    @Resource
    private RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;
    @Resource
    private RestDmItemClient restDmItemClient;


    /**
     * 根据用户Id返回当前用户常用购票人列表
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/v/queryLinkUser", method = RequestMethod.POST)
    public Dto queryLinkUser(@RequestParam(value = "userId", required = true) String userId) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userId);
        List<DmLinkUser> list=restDmLinkUserClient.getDmLinkUserListByMap(map);
        return DtoUtil.returnDataSuccess(list);
    }



    /**
     * 添加常用购票人
     *
     * @param dmLinkUser
     * @return
     */
    @RequestMapping(value = "/v/addLinkUser", method = RequestMethod.POST)
    public Dto queryLinkUser(@ModelAttribute("dmLinkUser") DmLinkUser dmLinkUser) throws Exception {
        dmLinkUser.setCreatedTime(new Date());
        dmLinkUser.setUpdatedTime(new Date());
        restDmLinkUserClient.qdtxAddDmLinkUser(dmLinkUser);
        return DtoUtil.returnSuccess();

    }


    /**
     * 验证购票人是否已经存在
     *
     * @param idCard
     * @return
     */
    @RequestMapping(value = "/v/queryLinkUserByIdCard", method = RequestMethod.POST)
    public Dto queryLinkUserByIdCard(String idCard) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("idCard",idCard);
        Integer count=restDmLinkUserClient.getDmLinkUserCountByMap(map);
        if(count==0||null==count){
            return DtoUtil.returnSuccess(ErrorCode.Purchase_tickets_No_Exit.getErrorMessage());
        }

        return DtoUtil.returnFail(ErrorCode.Purchase_tickets_Exit.getErrorMessage(),ErrorCode.Purchase_tickets_Exit.getErrorCode());

    }


    /**
     * 根据用户选择节目及座位，计算价格
     * @param itemId
     * @param schedulerId
     * @param seatPositions
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/v/queryItemPrice", method = RequestMethod.POST)
    public Dto queryItemPrice(String itemId,String schedulerId,String seatPositions) throws Exception {
        Map<String,Object> map=null;
        //=====1：根据节目id获取节目信息
        DmItem dmItem=restDmItemClient.getDmItemById(Long.parseLong(itemId));
        //=====2：拆分座位
        String[] seat=seatPositions.split(",");
        //=====3：根据排期id获取计算座位价格信息
        map=new HashMap<>();
        map.put("schedulerId",schedulerId);
        DmSchedulerSeatPrice dmSchedulerSeatPrice =restDmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(map).get(0);
        //座位总金额
        Double price=dmSchedulerSeatPrice.getPrice()*seat.length;
        StringBuffer  stringBuffer=new StringBuffer("{");
        stringBuffer.append("\"itemName\":\""+dmItem.getItemName()+"\",");
        stringBuffer.append("\"seatNames\":\""+seatPositions+"\",");
        stringBuffer.append("\"seatCount\":"+seat.length+",");
        stringBuffer.append("\"totalAmount\":"+price+"");
        stringBuffer.append("}");
        return DtoUtil.returnDataSuccess(stringBuffer);
    }

    /**
     * 下单接口
     * @param orderVo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/v/createOrder", method = RequestMethod.POST)
    public Dto queryItemPrice(@RequestBody CreateOrderVo orderVo) throws Exception {
        return orderService.createOrder(orderVo);
    }

    }
